﻿Public Class ProductionOrderExplorer
    Inherits ExplorerTreeList

    '========================================
    Private selectedRow As DataRow
    Private alot As PLBase
    Private LotPicking As PLBase
    Private frmCaption As String = String.Empty

    Private aPLName As String
    Private aMetaQueryName As String
    Private aSelectedGUID As Guid
    '========================================


    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
    End Sub

    Public Sub New()
        MyBase.New()
    End Sub

    Public Overrides Sub ActionOptionLoad()
        MyBase.ActionOptionLoad()
        Dim action As CommonExt.Base.ActionOption

        action = New ActionOption("IM", "ACT_00192", "Add New ProdOrder", AddressOf MenuNewProdOrder, True, True, "0")
        DefaultTreeListActionOptionList.Add(action)

    End Sub


    Public Overridable Sub MenuNewProdOrder(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        ' Memory management / Help the GC
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If

        If LotPicking IsNot Nothing Then
            LotPicking.Dispose()
            LotPicking = Nothing
        End If

        If alot IsNot Nothing Then
            alot.Dispose()
            alot = Nothing
        End If

        DetailProdOrderExplorer()


        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next


        Me.Cursor = Cursors.Default
    End Sub




    Public Overrides Sub PopulateDetailSection()
        Me.Cursor = Cursors.WaitCursor
        '
        Dim DescriptionLang As String = "_PRM"

        If AppManager.ApplicationOption.DescriptionColumnIndex = 3 Then
            DescriptionLang = "_ALT"
        Else
            DescriptionLang = "_PRM"
        End If

        aSelectedGUID = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
        aPLName = DirectCast(NodeDataRow("PL"), String)
        aMetaQueryName = DirectCast(NodeDataRow("MetaQuerie"), String)
        '
        ' Memory management / Help the GC
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If

        If LotPicking IsNot Nothing Then
            LotPicking.Dispose()
            LotPicking = Nothing
        End If

        If alot IsNot Nothing Then
            alot.Dispose()
            alot = Nothing
        End If
        '
        Select Case aMetaQueryName
            Case "Lot"
                frmCaption = DirectCast(NodeDataRow("LotDescr" & DescriptionLang), String)
                ShowAllProductionOrder()
            Case "LotExpress"
                If NodeDataRow.Table.Columns.Contains("LotDescr" & DescriptionLang) AndAlso NodeDataRow.Table.Columns.Contains("PrdDescr" & DescriptionLang) Then
                    frmCaption = String.Format("{0} - {1}", DirectCast(NodeDataRow("LotDescr" & DescriptionLang), String), DirectCast(NodeDataRow("PrdDescr" & DescriptionLang), String))
                Else
                    frmCaption = "LotDescr" & DescriptionLang + ", PrdDescr" & DescriptionLang
                End If

                DetailProdOrderExplorer()
            Case "LotPicking_LotBOM"
                If NodeDataRow.Table.Columns.Contains("PrdCd") AndAlso NodeDataRow.Table.Columns.Contains("PrdDescr" & DescriptionLang) Then
                    frmCaption = String.Format("{0} - {1}", DirectCast(NodeDataRow("PrdCd"), String), DirectCast(NodeDataRow("PrdDescr" & DescriptionLang), String))
                Else
                    frmCaption = "PrdCd, PrdDescr" & DescriptionLang
                End If

                DetailLotBOMForPicking()
            Case Else
        End Select
        '
        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next

        Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Sub ShowAllProductionOrder()
        alot = PLBase.CreateInstanceOf(PLTypeOf.Lot_PL, "LOT")
        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = alot, .SecureGrid = True, .Name = "LOT", .FormCtrlCD = MyBase.PanelName}

        'MyBase.PL.SQLWhere = String.Format("Lot.Crtd_DT > DATEADD(month, -12, getdate())")    '> '{}' '2012-01-01' ")
        'MyBase.PL.ID = aSelectedGUID
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Overridable Sub DetailProdOrderExplorer()
        alot = PLBase.CreateInstanceOf(PLTypeOf.Lot_PL, "LotExpress")


        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = alot, .Name = "Lot", .FormCtrlCD = MyBase.PanelName}
        aGridControl.SecureGrid = True
        '
        alot.SQLWhere = String.Format("LotID = '{0}'", aSelectedGUID)
        alot.ID = aSelectedGUID

        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewMasterRow
        AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewDetailRow
        AddHandler aGridControl.ShownMasterEditor, AddressOf ShownMasterEditor
        AddHandler aGridControl.ShownDetailEditor, AddressOf ShownDetailEditor
        AddHandler aGridControl.ProcessCurrentMasterRecord, AddressOf ProcessCurrentMasterRecord
        AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf ProcessCurrentDetailRecord
        AddHandler aGridControl.ValidateDetailRow, AddressOf ValidateDetailRow
        AddHandler aGridControl.DetailCustomRowCellEditForEditing, AddressOf CustomRowCellEditForEditing

        Dim action As ActionOption = New ActionOption("Lot", "ACT_00111", "Auto Picking", AddressOf MenuAutoPicking, True, True)
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("ActionALL", "ACT_00112", "Reserve ALL", AddressOf ActionReserveALL, True, True)
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("ActionALL", "ACT_00113", "Confirm ALL", AddressOf ActionConfirmALL, True, True)
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("ActionALL", "ACT_00114", "Release ALL", AddressOf ActionReleaseALL, True, True)
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("ActionPrint", "ACT_00159", "Fiche de Cueillette", AddressOf CreateLotDocument, True, True)
        aGridControl.GridActionOptionList.Add(action)



    End Sub
    '
    Public Overridable Sub DetailLotBOMForPicking()

        LotPicking = PLBase.CreateInstanceOf(PLTypeOf.LotTransaction_PL, "LotPicking_LotBOM")
        'Scoper nouveau PL en haut en haut        
        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = LotPicking, .Name = "LotPicking", .FormCtrlCD = MyBase.PanelName}
        aGridControl.SecureGrid = True
        '
        LotPicking.SQLWhere = String.Format("LotBOMID = '{0}'", aSelectedGUID)        '

        Dim action As ActionOption = New ActionOption("Action", "ACT_00115", "Reserve Line", AddressOf ActionReserveLine, True, True, "LotPicking_LotTransaction")
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("Action", "ACT_00116", "Confirm Line", AddressOf ActionConfirmLine, True, True, "LotPicking_LotTransaction")
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("Action", "ACT_00117", "Release Line", AddressOf ActionReleaseLine, True, True, "LotPicking_LotTransaction")
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("Action", "ACT_00118", "Revert Line", AddressOf ActionRevertLine, True, True, "LotPicking_LotTransaction")
        aGridControl.GridActionOptionList.Add(action)


        action = New ActionOption("Action", "ACT_00119", "Reserve components", AddressOf ActionReserveComponents, True, True)
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("Action", "ACT_00120", "Confirm components", AddressOf ActionConfirmComponents, True, True)
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("Action", "ACT_00121", "Release components", AddressOf ActionReleaseComponents, True, True)
        aGridControl.GridActionOptionList.Add(action)

        AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewDetailRow_Picking
        AddHandler aGridControl.ValidateDetailRow, AddressOf ValidateDetailRow_Picking
        AddHandler aGridControl.DetailCellValueChanged, AddressOf DetailCellValueChanged_Picking
        AddHandler aGridControl.DetailCustomRowCellEditForEditing, AddressOf DetailCustomRowCellEditForEditing_Picking
        AddHandler aGridControl.DeleteRow, AddressOf DeleteRow_Picking
        AddHandler aGridControl.DetailRowClick, AddressOf DetailRowClick_Picking

    End Sub

    ''' <summary>
    ''' Prepare to open the new Popup form for pass parameters to StoredProcedure
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub MenuAutoPicking(sender As Object, e As System.EventArgs)
        selectedRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

        If SCIEF.ActiveFramework.CommonExt.Base.AppManager.IsConnected Then
            Me.Cursor = Cursors.WaitCursor
            Dim newForm As XtraForm = New XtraForm() With {.Text = "Auto Picking", .Width = 800}
            Dim gridSP As GridExtSP = New GridExtSP("AutoPicking") With {.Dock = DockStyle.Fill}
            gridSP.SaveCMD = AddressOf AutoPicking

            AddHandler gridSP.AfterLoad, AddressOf AddRowParameter

            newForm.Controls.Add(gridSP)
            newForm.ShowDialog()
            Me.Cursor = Cursors.Default
        End If

    End Sub

    ''' <summary>
    ''' Create a new ROW to add in popup screen with good values for parameters
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub AddRowParameter(sender As Object, e As System.EventArgs)
        Try
            Dim aGrid As GridExtSP = CType(sender, GridExtSP)

            Dim arow As DataRow = aGrid.MetaDataTable.NewRow()
            'assign value for the metacolumn here

            arow("@plotid") = selectedRow("LotID")
            'arow("@pprdtypeid") = selectedRow("LotID")
            'arow("@ppickingtypeid") = selectedRow("LotID")
            'arow("@useAlternates") = cbool("1")

            aGrid.MetaDataTable.Rows.Add(arow)

        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    ''' <summary>
    ''' Launch the Stored Procedure
    ''' </summary>
    ''' <param name="aMetaDataTable"></param>
    ''' <remarks></remarks>
    Public Overridable Function AutoPicking(ByVal aMetaDataTable As MetaDataTable) As Boolean
        Try
            aMetaDataTable.ApplyChanges(reload:=False)
            MessageWindow.ShowMessage("Successfully picked.", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
        Return True
    End Function



End Class



